#include "SceneManager.h"

SceneManager::SceneManager(void)
{
	Init();
	m_objectTouch = new ObjectTouch();
	m_objectSeleted = NULL;
}
SceneManager::~SceneManager(void)
{
//	cout<<"Destructor"<<endl;
}
void SceneManager::AddObject(Object *object)
{
	this->m_ObjectList.push_back(object);
}
void SceneManager::Init()
{
	printf("\n Load data from SceneManager.txt file");
	FILE* pFile;
	fopen_s(&pFile, NAME_FILE_OBJECT , "r+");
	if(pFile != NULL)
	{
		int sizeObject = 0;
		float objectPosX;
		float objectPosY;
		int objectVelocity = 0;
		int objectID = 0;
		char objectType[MAX_SIZE_TYPE + 1];
		fscanf(pFile, OBJ_SIZE, &sizeObject);
		printf("Object size = %d \n", sizeObject);
		for(int i = 0 ; i < sizeObject; i++)
		{
			fscanf(pFile, OBJ_ID, &objectID);
			fscanf(pFile, OBJ_TYPE, &objectType);
			int  m_type = GetTypeObject(objectType);
			switch(m_type)
			{
			case RECTANGLE : 
				int m_width, m_height;
				objectPosX = objectPosY = 0;
				m_width = m_height = 0;
				fscanf(pFile, POS_RECT, &objectPosX, &objectPosY, &m_width, &m_height);
				printf("RECTANGLE: %f, %f, %d, %d \n",objectPosX, objectPosY, m_width, m_height);
				fscanf(pFile, OBJ_VELOCITY, &objectVelocity);
				printf("Velocity:: %d\n ", objectVelocity);
				Rectangles* rect;
				rect = new Rectangles(objectPosX, objectPosY, m_width, m_height);
				rect->SetVelocity(objectVelocity);
				this->AddObject(rect);
				break;
			case CIRCLE:
				int m_radius;
				m_radius = 0;
				objectPosX = objectPosY = 0;
				fscanf(pFile, POS_CIRCLE, &objectPosX, &objectPosY, &m_radius);
				printf("CIRCLE: %f, %f, %d \n",objectPosX, objectPosY, m_radius);
				fscanf(pFile, OBJ_VELOCITY, &objectVelocity);
				printf("Velocity:: %d\n ", objectVelocity);
				Circle* circle;
				circle = new Circle(objectPosX, objectPosY, m_radius);
				circle->SetVelocity(objectVelocity);
				this->AddObject(circle);
				break;
			case PLANE:
				int objectPosX2, objectPosY2;
				objectPosX = objectPosY = 0;
				objectPosX2 = objectPosY2 = 0;
				fscanf(pFile, POS_RECT, &objectPosX, &objectPosY, &objectPosX2, &objectPosY2);
				printf("PLANE: %d, %d, %d, %d \n",objectPosX, objectPosY, objectPosX2, objectPosY2);
				fscanf(pFile, OBJ_VELOCITY, &objectVelocity);
				printf("Velocity:: %d\n ", objectVelocity);
				Plane* plane;
				plane = new Plane();
				plane->SetPosition(objectPosX, objectPosY);
				plane->SetPositionEndPoint(objectPosX2, objectPosY2);
				plane->SetVelocity(objectVelocity);
				this->AddObject(plane);
				break;

			}
		}
		fclose(pFile);
		struct stat att;
		stat(NAME_FILE_OBJECT, &att);
		m_LastTimeFileModified = att.st_mtime;
	}else printf("File not found!!!\n");
}
void SceneManager::Render()
{
	list<Object*>:: iterator i = this->m_ObjectList.begin();
	while(i != this->m_ObjectList.end())
	{
		
		(*i)->Render();
		i++;
	}
	//Todo
}
void SceneManager::Update(float frameTime)
{
	struct stat attrib;
	stat(NAME_FILE_OBJECT, &attrib);
	if(attrib.st_mtime != m_LastTimeFileModified)
	Init();
}
EObjectType SceneManager::GetTypeObject(char* m_type)
{
	if(strcmp(m_type, TYPE_RECT) == 0)
	{
		return RECTANGLE;
	}else if(strcmp(m_type, TYPE_CIRCLE) == 0)
	{
		return CIRCLE;
	}else if(strcmp(m_type, TYPE_PLANE) == 0)
	{
		return PLANE;
	}
	else return NONE;

}
//Task 2
void SceneManager::TouchActionDown(ESContext *esContext, int x, int y)
{
	m_objectTouch->m_iStateTouch = TOUCH_DOWN;
	SetTouchPosition(x, y);
	list<Object*>::iterator m_obj = this->m_ObjectList.begin();
	for( ; m_obj != this->m_ObjectList.end(); m_obj++)
	{
		if((*m_obj)->InRect(x, y))
		{
			this->m_objectSeleted = *m_obj;
			this->m_objectSeleted->SetSelect(true);
			this->m_objectSeleted->SetVelocity(0, 0);

		}
	}
}
void SceneManager::TouchActionMove(ESContext *esContext, int x, int y)
{
	m_objectTouch->m_iStateTouch = TOUCH_MOVE;
	this->SetTouchPosition(x, y);
	if(this->m_objectSeleted->IsSelected())
	{
		this->m_objectSeleted->MoveObjectAround(m_objectTouch);
		this->m_objectSeleted->SetDestPosition(x, y);
	}
}
	
void SceneManager::TouchActionUp(ESContext *esContext, int x, int y)
{
	m_objectTouch->m_iStateTouch = TOUCH_UP;
	SetTouchPosition(x, y);
	if(this->m_objectSeleted->IsSelected())
	{
		this->m_objectSeleted->SetSelect(false);
		this->m_objectSeleted->SetDestPosition(-1, -1);
		this->m_objectSeleted = NULL;
	}

}
void SceneManager::SetTouchPosition(int posX, int posY)
{
	m_objectTouch->m_iPrePosX = m_objectTouch->m_iCurrentPosX;
	m_objectTouch->m_iPrePosY = m_objectTouch->m_iCurrentPosY;
	m_objectTouch->m_iCurrentPosX = posX;
	m_objectTouch->m_iCurrentPosY = posY;

}
